package cn.com.dcsgo.controller;

import cn.com.dcsgo.api.dto.NotificationDTO;
import cn.com.dcsgo.domain.entity.Notification;
import cn.com.dcsgo.service.NotificationService;
import cn.com.dcsgo.util.NotificationConverter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * NotificationController
 *
 * @author Dcsgo
 * @since 2025/4/3 15:10
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "通知接口")
@RequestMapping("api/notification")
public class NotificationController {
    private final NotificationService notificationService;

    @PostMapping
    @ApiOperation("添加通知")
    public boolean add(@RequestBody NotificationDTO notificationDTO) {
        if (notificationDTO.getUserId().equals(0L)) {
            try {
                Thread.sleep(3000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        return notificationService.save(NotificationConverter.convert(notificationDTO));
    }

    @PutMapping
    @ApiOperation("更新通知")
    public boolean updateById(@RequestBody NotificationDTO notificationDTO) {
        return notificationService.updateById(NotificationConverter.convert(notificationDTO));
    }

    @GetMapping
    @ApiOperation("获取所有通知")
    public List<NotificationDTO> getAll() {
        return notificationService.list().stream().map(NotificationConverter::convert).toList();
    }

    @GetMapping("userId")
    @ApiOperation("根据用户ID获取通知")
    public List<NotificationDTO> getByUserId(@RequestParam("userId") Long userId, @RequestParam("isRight") Boolean isRight) {
        if (!isRight) {
            throw new RuntimeException("错误请求");
        }
        List<Notification> notifications = notificationService.getByUserId(userId);
        return notifications.stream().map(NotificationConverter::convert).toList();
    }
}
